<?php
if(!$_GET['city'])exit;
if (!function_exists('mb_ucfirst') && function_exists('mb_substr')) {
     function mb_ucfirst($string) {
          $string = mb_ereg_replace("^[\ ]+","", $string);
          $string = mb_strtoupper(mb_substr($string, 0, 1, "UTF-8"), "UTF-8").mb_substr($string, 1, mb_strlen($string), "UTF-8" );
          return $string;
     }
}
include('../class/class_connect.php');
include('../class/class_checkAddress.php');
$aConnect=new connect('cs');
$checkAddress=new checkAddress();
function checkConvert($value)
{
	$value=preg_replace("/[\. \/(\)\-]/", "", $value);
	//$value = iconv("UTF-8", "WINDOWS-1251", $value);
	$value=addslashes($value);
	$value=htmlspecialchars($value);
	return($value);
}
//$connect->connect('cs');
//$street=iconv("UTF-8", "WINDOWS-1251", $_GET['s']);
$street=$_GET['s'];
$building=checkConvert($_GET['b']);
$number=checkConvert($_GET['number']);
$city=checkConvert($_GET['city']);
//$b=$_GET['b'];
//$s = preg_replace("/[\. \(\)\-]/", "", $s);
//$b= = preg_replace("/[\. \(\)\-]/", "", $b);
//$city=$_GET['city'];
//$s = iconv("UTF-8", "WINDOWS-1251", $s);
//$b = iconv("UTF-8", "WINDOWS-1251", $b);
if($street && !$building && !$number)
{
	if(!empty($city))
	{
		if(strlen($street))
		{
			$s=mb_strtolower($street, 'UTF-8');
			$s=mb_ucfirst($s);
			$s=$checkAddress->checkStreet($s);
			if($s)
			{
				$query="SELECT * FROM $city WHERE street LIKE '$s%'";
				$res=mysql_query($query) or die(mysql_error());
				if($num_rows=mysql_num_rows($res))
				{
					while($row=mysql_fetch_array($res))
					{
						$streets[]=$row['street'];
					}
					if($streets)
					{
						if(count($streets)==1 && $streets[0]==$s)
							exit;
						for($i=0; $i<count($streets)&&$i<10; $i++)
						{
							echo ('
								<div class="streetResult">'.$streets[$i].'</div>
							');
						}
					}
				}
			}
		}
	}
}
if($city && $street && $building && !$number)
{
	$aConnect=new connect('cs');
	$street=$checkAddress->checkStreet($street);
	$building=$checkAddress->checkBuilding($building);
	if($building)
	{
		$row=mysql_fetch_array($res=$aConnect->select_all($city, "WHERE street='".$street."'"));
		if($row)
		{
			$streetBuildings=explode(',', $row['buildings']);
			foreach($streetBuildings as $key=>$aBuilding)
			{
				$match="/^".str_replace('/', ' ', $building)."/";
				if(preg_match($match, str_replace('/', ' ',$aBuilding)))
				{
					$matchedBuilding[]=mb_strtolower($aBuilding, 'UTF-8');
				}
			}
			$num=count($matchedBuilding);
			if($num>1 ||($num==1 && $matchedBuilding[0]!=$building))
			{
				for($i=0; $i<count($matchedBuilding)&&$i<15; $i++)// as $key=>$aBuildingRes)
				{
					echo ('
						<div class="buildingResult">'.$matchedBuilding[$i].'</div>
					');
				}
			}
		}
	}

		/*while($row=mysql_fetch_array($res))
		{
			if($row['building']==$building && $num==1)
				checkMall($city, $street, $building);
			elseif($num)
			{
				echo ('
				<div class="buildingResult">
					'.$row['building'].'
				</div>
				');
			}
		}
	}


	//if(!$aMallIsThere)
	//{

		/*$aConnect=new connect('cs');
		//$responce='building^'.$building;
		if(strlen($building) && $city)
		{
			$query="SELECT * FROM $city WHERE street='$street'";
			//echo $query;
			$res=mysql_query($query) or die(mysql_error());
			$row=mysql_fetch_array($res);
				//echo $row['buildings'];
				$match="/^".$building."/";
				$buildings=explode(',', $row['buildings']);
				for($i=0; $i<count($buildings); $i++)
				{

					if(preg_match($match, $buildings[$i]))
					{

						$matchedBuildings[]=$buildings[$i];
					}
				}
				if(count($matchedBuildings)==1 && $matchedBuildings[0]==$building && !$aMallIsThere)
					//echo $matchedBuildings[0];
						exit;
				if(count($matchedBuildings)==1 && $aMallIsThere && $matchedBuildings[0]==$building)
		        {
					$row=mysql_fetch_array($res);
					$responce='mall^'.$row['short'].' '.$row['name'].'<input name="mallSuggest'.$number.'" type="checkbox" value="'.$row['id'].','.$row['name'].','.$row['short'].'">';
					echo $responce;
				}
				else
				{
				//if(count($matchedBuildings)==1 && $matchedBuildings[0]==$building)
					//echo $matchedBuildings[0];
						//exit;
					if(count($matchedBuildings))
					{
						echo "building^";
						for($i=0; $i<count($matchedBuildings); $i++)
						{
							echo ('
							<div class="buildingResult">'.$matchedBuildings[$i].'</div>
							');
						}
					}
				}
					//$matchedBuildings=implode(',', $matchedBuildings);
				//$responce=$matchedBuildings;



			//echo $responce;
		}



          */

}
if($city && $street && $_GET['building'] && $number)
{
	$building=checkConvert($_GET['building']);
	$aConnect=new connect('m');
	$street=$checkAddress->checkStreet($street);
	$building=$checkAddress->checkBuilding($building);
	$address=$street.', '.$building;
	$res=$aConnect->select_all($city, "WHERE address='$address'");
	$aMallIsThere=mysql_num_rows($res);
	if($aMallIsThere)
	{
		$row=mysql_fetch_array($res);
		echo $row['short'].' '.$row['name'].'<input name="mallSuggest'.$number.'" type="checkbox" value="'.$row['id'].','.$row['name'].','.$row['short'].'" checked>';
	}
}
?>